﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.BackgroundAudio;
using System.Windows.Media.Imaging;

namespace XiaMiRingtone
{
    public partial class lingyin : PhoneApplicationPage
    {
        public lingyin()
        {
            InitializeComponent();
            CreatDir();
            SetLB();
            SetSkin();
        }

        private void SetSkin()
        {
            var store = IsolatedStorageFile.GetUserStoreForApplication();
            if (store.FileExists("skin.jpg"))
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("skin.jpg", FileMode.Open, store))
                {
                    BitmapImage bi = new BitmapImage();
                    bi.SetSource(stream);
                    Ib.ImageSource = bi;
                }
            }
        }

        void SetLB()
        {
            var store = IsolatedStorageFile.GetUserStoreForApplication();

            string[] lsfiles = store.GetFileNames("rings//*.mp3");
            llb.ItemsSource = lsfiles;

            string[] files = store.GetFileNames("*.mp3");
            listbox1.ItemsSource = files;

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string header = (sender as MenuItem).Header.ToString();

            ListBoxItem selectedListBoxItem = this.listbox1.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
            string fn = selectedListBoxItem.Content.ToString();

            var store = IsolatedStorageFile.GetUserStoreForApplication();

            if (selectedListBoxItem == null)
            {
                return;
            }
            
            if (header == "删除")
            {
                store.DeleteFile(fn);
                SetLB();
            }
            else
            {
                NavigationService.Navigate(new Uri("/editring.xaml?name="+fn, UriKind.Relative));
            }
        }

        private void Ring_Click(object sender, RoutedEventArgs e)
        {
            string header = (sender as MenuItem).Header.ToString();

            ListBoxItem selectedListBoxItem = this.listbox1.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
            string fn = selectedListBoxItem.Content.ToString();

            var store = IsolatedStorageFile.GetUserStoreForApplication();

            if (selectedListBoxItem == null)
            {
                return;
            }

            if (header == "删除")
            {
                store.DeleteFile("rings//"+fn);
                SetLB();
            }
            else
            {
                SetRing(fn);
            }
        }

        void SetRing(string filename)
        {
            SaveRingtoneTask saveRingtoneChooser = new SaveRingtoneTask();
            saveRingtoneChooser.Completed += new EventHandler<TaskEventArgs>(saveRingtoneChooser_Completed);

            try
            {
                saveRingtoneChooser.Source = new Uri("isostore:/rings//" + filename);

                saveRingtoneChooser.DisplayName = filename;

                saveRingtoneChooser.Show();
            }
            catch (System.InvalidOperationException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void saveRingtoneChooser_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                //Logic for when the ringtone was saved successfully
                case TaskResult.OK:
                    MessageBox.Show("铃声保存成功！");
                    break;

                //Logic for when the task was cancelled by the user
                case TaskResult.Cancel:
                    MessageBox.Show("铃声无法保存！");
                    break;

                //Logic for when the ringtone could not be saved
                case TaskResult.None:
                    MessageBox.Show("铃声无法保存！");
                    break;
            }
        }

        void CreatDir()
        {
            var store = IsolatedStorageFile.GetUserStoreForApplication();

            if (store.DirectoryExists("temp") == false)
            {
                store.CreateDirectory("temp");
            }
            if (store.DirectoryExists("rings") == false)
            {
                store.CreateDirectory("rings");
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            SetLB();
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            //BackgroundAudioPlayer.Instance.Close();
            base.OnNavigatedFrom(e);
        }
    }
}